from rest_framework.test import APITestCase
from rest_framework import status
from django.urls import reverse
from .models import Room

class RoomListViewTest(APITestCase):
    def setUp(self):
        # 确保每次测试前数据库是干净的，并且有默认房间
        Room.objects.all().delete()
        default_rooms = ["客厅", "厨房", "客房", "卧室", "卫生间", "阳台"]
        for room_name in default_rooms:
            Room.objects.create(name=room_name)

    def test_get_room_list(self):
        """
        测试获取房间列表的API
        """
        url = reverse('room-list') # 假设你的URL名称是room-list
        response = self.client.get(url)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response.data), 6) # 默认有6个房间

        expected_room_names = ["客厅", "厨房", "客房", "卧室", "卫生间", "阳台"]
        returned_room_names = [room['name'] for room in response.data]
        self.assertCountEqual(returned_room_names, expected_room_names)

        # 检查每个返回的房间数据是否包含room_id和name字段
        for room in response.data:
            self.assertIn('room_id', room)
            self.assertIn('name', room)
            self.assertIsInstance(room['room_id'], int)
            self.assertIsInstance(room['name'], str)
